import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 登录面板
 */
public class LoginPanel extends JPanel {
    private HotelManagementSystem mainFrame;
    private JTextField usernameField;
    private JPasswordField passwordField;
    private JButton loginButton;
    private JButton changePasswordButton;

    public LoginPanel(HotelManagementSystem mainFrame) {
        this.mainFrame = mainFrame;
        setupUI();
    }

    private void setupUI() {
        setLayout(new BorderLayout());
        setBackground(HotelManagementSystem.getBackgroundColor());

        // 创建主面板
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBackground(HotelManagementSystem.getBackgroundColor());
        mainPanel.setBorder(new EmptyBorder(50, 50, 50, 50));

        GridBagConstraints gbc = new GridBagConstraints();

        // 标题
        JLabel titleLabel = new JLabel("酒店房间预定管理系统");
        titleLabel.setFont(HotelManagementSystem.getTitleFont());
        titleLabel.setForeground(HotelManagementSystem.getPrimaryColor());
        titleLabel.setHorizontalAlignment(SwingConstants.CENTER);
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.insets = new Insets(0, 0, 30, 0);
        mainPanel.add(titleLabel, gbc);

        // 登录面板
        JPanel loginPanel = createLoginPanel();
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.insets = new Insets(0, 0, 0, 0);
        mainPanel.add(loginPanel, gbc);

        add(mainPanel, BorderLayout.CENTER);
    }

    private JPanel createLoginPanel() {
        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBackground(Color.WHITE);
        panel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(HotelManagementSystem.getPrimaryColor(), 2),
                new EmptyBorder(30, 30, 30, 30)));

        GridBagConstraints gbc = new GridBagConstraints();

        // 登录标题
        JLabel loginTitle = new JLabel("管理员登录");
        loginTitle.setFont(new Font("微软雅黑", Font.BOLD, 18));
        loginTitle.setForeground(HotelManagementSystem.getPrimaryColor());
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.insets = new Insets(0, 0, 20, 0);
        panel.add(loginTitle, gbc);

        // 用户名
        JLabel usernameLabel = new JLabel("用户名:");
        usernameLabel.setFont(HotelManagementSystem.getButtonFont());
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.WEST;
        gbc.insets = new Insets(5, 0, 5, 10);
        panel.add(usernameLabel, gbc);

        usernameField = new JTextField(15);
        usernameField.setFont(HotelManagementSystem.getButtonFont());
        usernameField.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(Color.GRAY),
                new EmptyBorder(5, 5, 5, 5)));
        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add(usernameField, gbc);

        // 密码
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setFont(HotelManagementSystem.getButtonFont());
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.NONE;
        panel.add(passwordLabel, gbc);

        passwordField = new JPasswordField(15);
        passwordField.setFont(HotelManagementSystem.getButtonFont());
        passwordField.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(Color.GRAY),
                new EmptyBorder(5, 5, 5, 5)));
        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        panel.add(passwordField, gbc);

        // 按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout());
        buttonPanel.setBackground(Color.WHITE);

        loginButton = createStyledButton("登录", HotelManagementSystem.getPrimaryColor());
        loginButton.addActionListener(new LoginActionListener());
        buttonPanel.add(loginButton);

        changePasswordButton = createStyledButton("修改密码", HotelManagementSystem.getSecondaryColor());
        changePasswordButton.addActionListener(new ChangePasswordActionListener());
        buttonPanel.add(changePasswordButton);

        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.gridwidth = 2;
        gbc.insets = new Insets(20, 0, 0, 0);
        gbc.fill = GridBagConstraints.NONE;
        panel.add(buttonPanel, gbc);

        // 添加回车键监听
        passwordField.addActionListener(new LoginActionListener());

        return panel;
    }

    private JButton createStyledButton(String text, Color backgroundColor) {
        JButton button = new JButton(text);
        button.setFont(HotelManagementSystem.getButtonFont());
        button.setBackground(backgroundColor);
        button.setForeground(Color.WHITE);
        button.setBorder(new EmptyBorder(8, 15, 8, 15));
        button.setFocusPainted(false);
        button.setCursor(new Cursor(Cursor.HAND_CURSOR));

        // 添加鼠标悬停效果
        button.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                button.setBackground(backgroundColor.darker());
            }

            public void mouseExited(java.awt.event.MouseEvent evt) {
                button.setBackground(backgroundColor);
            }
        });

        return button;
    }

    private class LoginActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String username = usernameField.getText().trim();
            String password = new String(passwordField.getPassword());

            if (username.isEmpty() || password.isEmpty()) {
                JOptionPane.showMessageDialog(LoginPanel.this, "请输入用户名和密码！", "提示", JOptionPane.WARNING_MESSAGE);
                return;
            }

            if (DataManager.getInstance().validateAdmin(username, password)) {
                JOptionPane.showMessageDialog(LoginPanel.this, "登录成功！", "成功", JOptionPane.INFORMATION_MESSAGE);
                mainFrame.showMainMenu();
            } else {
                JOptionPane.showMessageDialog(LoginPanel.this, "用户名或密码错误！", "错误", JOptionPane.ERROR_MESSAGE);
                passwordField.setText("");
            }
        }
    }

    private class ChangePasswordActionListener implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            ChangePasswordDialog dialog = new ChangePasswordDialog(mainFrame);
            dialog.setVisible(true);
        }
    }
}